<template>
  <view class="wrap">
    <scroll-view
      :scroll-top="state.scrollTop"
      scroll-y="true"
      class="scroll-Y scroll"
      @scroll="scroll"
      id="view1"
    >
      <view>
        <view class="header headerName">
          <img class="img" src="https://ainengli.hzjrsw.com/jkhx/knowledgeBg.png" alt="" />
          <view class="title">关于血脂</view>
          <view class="subtitle">了解健康知识，我的健康我做主</view>
        </view>
        <scroll-view :class="['scroll-X', state.scrollActive == true ? 'on' : '']" scroll-x="true">
          <view class="tabList">
            <view
              :class="['item', state.tabActive == index ? 'on' : '']"
              v-for="(item, index) in state.tabList"
              :key="index"
              @tap="tabFun(index)"
            >
              <text class="text">{{ item.name }}</text>
              <text class="text" />
            </view>
          </view>
        </scroll-view>
        <view class="content itemName itemName1">
          <view class="title">
            <text class="name">概述</text>
            <img class="img" src="https://ainengli.hzjrsw.com/jkhx/common/title-bg-row.png" alt="" />
          </view>
          <text class="content-item">
            血脂是血浆中的中性脂肪（甘油三酯）和类脂（磷脂、糖脂、固醇、类固醇）的总称，广泛存在于人体中。它们是生命细胞的基础代谢必需物质。一般说来，血脂中的主要成分是甘油三酯和胆固醇，其中甘油三酯参与人体内能量代谢，而胆固醇则主要用于合成细胞浆膜、类固醇激素和胆汁酸。
          </text>
        </view>
        <view class="content itemName itemName2">
          <view class="title">
            <text class="name">正常范围</text>
            <img class="img" src="https://ainengli.hzjrsw.com/jkhx/common/title-bg-row.png" alt="" />
            <text class="unit"> 单位：(mmol/L) </text>
          </view>
          <view class="table-box">
            <view class="rows">
              <view class="rows-item rows-header">分层标准</view>
              <view class="rows-item rows-header">总胆固醇</view>
              <view class="rows-item rows-header">低密度脂蛋白胆固醇</view>
              <view class="rows-item rows-header">高密度脂蛋白胆固醇</view>
              <view class="rows-item rows-header">甘油三酯</view>
            </view>
            <view class="rows">
              <view class="rows-item">正常值</view>
              <view class="rows-item">{{ '2.2-5.7' }}</view>
              <view class="rows-item">0-3.6</view>
              <view class="rows-item">0.9-1.7</view>
              <view class="rows-item">0.2-1.7</view>
            </view>
            <view class="rows">
              <view class="rows-item">合适水平</view>
              <view class="rows-item">{{ '<5.2' }}</view>
              <view class="rows-item">{{ '<3.4' }}</view>
              <view class="rows-item" />
              <view class="rows-item">{{ '<1.7' }}</view>
            </view>
            <view class="rows">
              <view class="rows-item">理想水平</view>
              <view class="rows-item" />
              <view class="rows-item">{{ '<2.6' }}</view>
              <view class="rows-item" />
              <view class="rows-item">{{ '<1.7' }}</view>
            </view>
          </view>
          <text class="content-item"
            >上述各指标数值因各个医疗单位检测方法、实验条件差异可出现不完全相同的正常值。
          </text>
        </view>
        <view class="content itemName itemName3">
          <view class="title">
            <text class="name">异常范围</text>
            <img class="img" src="https://ainengli.hzjrsw.com/jkhx/common/title-bg-row.png" alt="" />
            <text class="unit"> 单位：(mmol/L) </text>
          </view>
          <view class="table-box">
            <view class="rows">
              <view class="rows-item rows-header">分层标准</view>
              <view class="rows-item rows-header">总胆固醇</view>
              <view class="rows-item rows-header">低密度脂蛋白胆固醇</view>
              <view class="rows-item rows-header">高密度脂蛋白胆固醇</view>
              <view class="rows-item rows-header">甘油三酯</view>
            </view>
            <view class="rows">
              <view class="rows-item">边缘升高</view>
              <view class="rows-item">5.2-6.2</view>
              <view class="rows-item">3.4-4.1</view>
              <view class="rows-item" />
              <view class="rows-item">1.7-2.3</view>
            </view>
            <view class="rows">
              <view class="rows-item">升高</view>
              <view class="rows-item">≥6.2</view>
              <view class="rows-item">≥4.1</view>
              <view class="rows-item" />
              <view class="rows-item">≥2.3</view>
            </view>
            <view class="rows">
              <view class="rows-item">降低</view>
              <view class="rows-item" />
              <view class="rows-item" />
              <view class="rows-item">{{ '<1.0' }}</view>
              <view class="rows-item" />
            </view>
          </view>
        </view>
        <view class="content itemName itemName4">
          <view class="title">
            <text class="name">就医指征</text>
            <img class="img" src="https://ainengli.hzjrsw.com/jkhx/common/title-bg-row.png" alt="" />
          </view>
          <text class="content-item"
            >发现血脂异常时，医生会重点关注患者是否有冠心病、脑血管疾病和周围动脉疾病的症状，如胸痛、气短、虚弱、呼吸困难或跛行，询问患者的家族史和饮食、运动情况，有助诊断和治疗。
          </text>
          <text class="content-item">
            就诊科室:
            血脂异常现属多学科就诊模式，对于体检发现血脂异常、有黄色瘤等相关典型症状者，优先考虑心内科或内分泌科就诊。在基层医院也可以选择全科医学科或内科就诊。有的患者由于脂质长期沉积、动脉粥样硬化，出现胸闷、气短等心脑血管疾病症状时，可选择心内科就诊。
          </text>
        </view>
        <view class="content itemName itemName5">
          <view class="title">
            <text class="name">日常生活建议</text>
            <img class="img" src="https://ainengli.hzjrsw.com/jkhx/common/title-bg-row.png" alt="" />
          </view>
          <text class="content-item"
            >1.饮食：减少饱和脂肪酸和胆固醇摄入，补充植物固醇和可溶性纤维。限制油脂摄入总量，20~25g/d。采用不饱和脂肪酸（植物油）替代饱和脂肪酸（动物油、棕榈油等）。避免摄入反式脂肪酸。增加蔬菜和水果、全谷物、膳食纤维及鱼类的摄入。
          </text>
          <text class="content-item">2.运动：坚持运动，控制体重，达到或接近标准体重。 </text>
          <text class="content-item">3.其他：戒烟，限制盐摄入量，限制饮酒，禁饮烈性酒。 </text>
        </view>
        <view class="content itemName itemName6">
          <view class="title">
            <text class="name">参考文献</text>
            <img class="img" src="https://ainengli.hzjrsw.com/jkhx/common/title-bg-row.png" alt="" />
          </view>
          <text class="content-item"
            >[1]中国成人血脂异常防治指南（2016年修订版）[J]中华心血管病杂志2016年10月
            第44卷第10期，833-846
          </text>
          <text class="content-item"
            >[2]王辰,王建安.内科学.第3版[M].北京人民卫生出版社,2015:1129-1134
          </text>
          <text class="content-item"
            >[3]葛均波,徐永建,王辰.内科学[M].第9版北京:人民卫生出版社,2018:861-864
          </text>
          <text class="content-item"
            >[4]MOZAFFARIAN D，MICHA R,WALLACE S.Effects on coronary heart disease of increasing
            polyunsaturated fat in place of saturated fat： a systematic review and meta-analysis of
            randomized controlled trials[J].PLoSMed，2010，7（3）：
            e1000252.DOI：10.1371/journal.pmed.1000252.
          </text>
          <text class="content-item">[5]中国血脂管理指南（基层版2024年） </text>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script setup lang="ts">
import { ref, reactive, nextTick } from 'vue'
import { onLoad, onUnload, onShow } from '@dcloudio/uni-app'
const state = reactive({
  tabList: [
    { name: '概述' },
    { name: '正常范围' },
    { name: '异常范围' },
    { name: '就医指征' },
    { name: '日常生活建议' },
    { name: '参考文献' }
  ],
  scrollActive: false,
  tabActive: 0,
  title: '',
  type: '',
  scrollTop: 0
})

const distanceArr = ref([])

onShow(() => {
  nextTick(() => {
    getDistanceArr()
  })
})

// 获取所有元素在当前页面所处的位置信息
const getDistanceArr = () => {
  uni
    .createSelectorQuery()
    .select('.itemName1')
    .boundingClientRect((res) => {
      distanceArr.value.push(res.top)
    })
    .exec()

  uni
    .createSelectorQuery()
    .select('.itemName2')
    .boundingClientRect((res) => {
      distanceArr.value.push(res.top)
    })
    .exec()
  uni
    .createSelectorQuery()
    .select('.itemName3')
    .boundingClientRect((res) => {
      distanceArr.value.push(res.top)
    })
    .exec()
  uni
    .createSelectorQuery()
    .select('.itemName4')
    .boundingClientRect((res) => {
      distanceArr.value.push(res.top)
    })
    .exec()

  uni
    .createSelectorQuery()
    .select('.itemName5')
    .boundingClientRect((res) => {
      distanceArr.value.push(res.top)
    })
    .exec()
  uni
    .createSelectorQuery()
    .select('.itemName6')
    .boundingClientRect((res) => {
      distanceArr.value.push(res.top)
    })
    .exec()
  uni
    .createSelectorQuery()
    .select('.itemName7')
    .boundingClientRect((res) => {
      distanceArr.value.push(res.top)
    })
    .exec()
}

const tabFun = (index) => {
  // 方法一 简单一点直接跳转到id对应的位置
  nextTick(() => {
    if (index == 0) {
      state.scrollTop = 0
      state.tabActive = 0
    } else {
      state.tabActive = index
      if (uni.getSystemInfoSync().uniPlatform == 'mp-weixin') {
        state.scrollTop = distanceArr.value[index] - 105
      } else {
        state.scrollTop = distanceArr.value[index] - 65
      }
    }
    const timer = setTimeout(() => {
      clearTimeout(timer)
    }, 500) // 解决ios和安卓、鸿蒙系统兼容性问题
  })
}
const scroll = (e) => {
  const skewY = 170 // 偏移量，由于吸顶的tab、头部的显示信息也有高度，素以做了偏移量
  if (e.detail.scrollTop >= skewY) {
    if (!state.scrollActive && state.tabActive <= 0) {
      // 在未显示tab并且 currentTab <= 0时，防止uview ui抖动bug，设置默认复位值
      state.tabActive = 0
    }
    state.scrollActive = true

    nextTick(() => {
      const length = distanceArr.value.length
      const index = distanceArr.value.findIndex((el) => el - skewY - e.detail.scrollTop > 0)
      // 当index  == -1 的时候，实际当前滚动的距离超出了最大值，也就是在最后一个tab显示的内容
      // 当index > 0 的时候，说明能在当前的scrollTop值找到，即index的前一位
      if (e.detail.scrollTop + 600 > distanceArr.value[length - 1]) {
        return
      } else {
        state.tabActive = index > 0 ? index - 1 : length - 1
      }
    })
  } else {
    state.scrollActive = false
  }
}
</script>

<style lang="scss" scoped>
.wrap {
  .header {
    position: relative;
    display: inline-block;
    width: calc(100% - 64rpx);
    height: 180rpx;
    margin: 32rpx 32rpx 0;

    .img {
      position: absolute;
      top: 0;
      width: 100%;
      height: 100%;
    }

    .title {
      position: relative;
      z-index: 1;
      margin: 30rpx 40rpx 20rpx;
      font-size: 40rpx;
      font-weight: 700;
      color: #fff;
    }

    .subtitle {
      position: relative;
      z-index: 1;
      margin: 0 40rpx;
      font-size: 28rpx;
      color: #fff;
    }
  }

  .scroll-Y {
    box-sizing: border-box;
    height: 100vh;
    padding-bottom: calc(30rpx + constant(safe-area-inset-bottom));
    padding-bottom: calc(30rpx + env(safe-area-inset-bottom));
  }

  .scroll-X {
    width: calc(100% - 64rpx);
    margin: 30rpx 32rpx 0;
    overflow-x: scroll;
    background: #f3f7f8;

    &.on {
      position: fixed;
      top: 0;
      left: 0;
      z-index: 9999;
      display: block;
      width: 100%;
      padding: 30rpx 0;
      margin: 0;

      .tabList {
        padding: 0 16rpx;
      }
    }

    .tabList {
      white-space: nowrap;

      .item {
        position: relative;
        display: inline-block;
        height: 64rpx;
        padding: 0 30rpx;
        margin-right: 32rpx;
        font-size: 28rpx;
        line-height: 64rpx;
        color: #222;
        text-align: center;
        vertical-align: middle;
        background: #fff;
        border-radius: 32rpx;

        &:nth-last-child(1) {
          margin-right: 0;
        }

        &.on {
          font-weight: 700;
          color: #fff;
          background: #00d1b6;
        }
      }
    }
  }

  .content {
    padding: 32rpx;
    margin: 30rpx 32rpx 0;
    background: #fff;
    border-radius: 16rpx;

    .title {
      position: relative;
      margin-bottom: 20rpx;

      .name {
        position: relative;
        z-index: 1;
        font-size: 34rpx;
        font-weight: 700;
        color: #222;
      }

      .img {
        position: absolute;
        bottom: 0;
        left: 0;
        display: block;
        width: 40rpx;
        height: 12rpx;
      }

      .unit {
        position: absolute;
        top: 50%;
        right: 0;
        font-size: 24rpx;
        color: #666;
        transform: translateY(-50%);
      }
    }

    .content-item {
      display: block;
      margin-top: 20rpx;
      font-size: 30rpx;
      line-height: 1.6;
      color: #222;
      text-align: justify;
      word-break: break-all;
    }

    .define {
      padding: 10px 0 10px 10px;
      margin: 20px 0 10px;
      font-size: 32rpx;
      font-weight: 700;
      background: linear-gradient(90deg, rgb(2 188 195 / 5%) 0%, rgb(2 188 195 / 0%) 100%);
      border-radius: 4px;
    }

    .itemName {
      margin-bottom: 40rpx;
    }

    .itemName:last-child {
      margin-bottom: 0;
    }
  }
}

.table-box {
  width: 100%;
  border: 1rpx solid #cce4e1;

  .rows {
    display: flex;
    font-size: 28rpx;

    .rows-item {
      display: flex;
      align-items: center;
      width: 100%;
      padding: 24rpx 0rpx 24rpx 16rpx;
      font-size: 24rpx;
      color: #222;
      border-right: 1rpx solid #cce4e1;
      // font-weight: 400;
      border-bottom: 1rpx solid #cce4e1;
    }

    .rows-item:last-child {
      border-right-width: 0rpx;
    }

    .rows-header {
      padding: 24rpx 16rpx;
      font-weight: 600;
      color: #00d1b6;
      background: rgb(0 209 182 / 5%);
    }
  }

  .rows:last-child {
    .rows-item {
      border-bottom-width: 0rpx;
    }
  }
}
</style>
